<?php
include_once __DIR__ . '/db_bb_connect.php';

require_once __DIR__ . '/includes/BasketbalHandlerFunctions.php';

$basketbalhandlerfunctions = new BasketbalHandlerFunctions();

?>

<div id="formframe"> 

<div class="container-fluid">
<div class="row">
<div class="col-lg-3 col-md-3"></div>
<div class="col-lg-6 col-md-6"> 
<h2>Add Players to Opponents Teams</h2>

	<form method="post" action="" id="form"> 

		<select name="season" class="form-control" id="select">

<?php if (isset($_POST['season'])) {

 $season = $_POST['season'];
 	echo "<option id=\"selected-season\" value=\"$season\" selected=\"selected\">$season is selected</option>";
 	
 } else {
 	echo "<option id=\"selected-season\" value=\"\" disabled=\"disabled\" selected=\"selected\">Select a season</option>";
 }

 ?>
 
         
<?php

$sql = "SELECT DISTINCT season FROM seasons ORDER BY season DESC";

$result = $db->query ($sql);

while ($row = $result->fetch_assoc()) {

    $season = $row['season'];
    echo "<option value=\"$season\"> $season </option>";
  
}
?>
  </select>
			<br>

		<select name="teamname" class="form-control" id="select">

<?php if (isset($_POST['season']) && isset($_POST['teamname'])) {

 $teamname = $_POST['teamname'];

 	echo "<option id=\"selected-season\" value=\"$teamname\" selected=\"selected\">$teamname is selected</option>";
 	
 } else {
 	echo "<option id=\"selected-season\" value=\"\" disabled=\"disabled\" selected=\"selected\">Select a team</option>";
 }

 ?>
 
         
<?php

$sql = "SELECT DISTINCT teamname FROM teams_list";

$result = $db->query ($sql);

while ($row = $result->fetch_assoc()) {
    
    $teamname = $row['teamname'];
    echo "<option value=\"$teamname\"> $teamname </option>"; 

}
?>			
		</select>
		<br>

		<select name="playerID" class="form-control" id="select">

<?php if (isset($_POST['season']) && isset($_POST['teamname']) && isset($_POST['playerID']) ) {

	$playerID_selected = $_POST['playerID'];

	$sql2 = "SELECT firstname, lastname FROM users WHERE user_key = '$playerID_selected' ";

	$result2 = $db->query ($sql2);
	$row2 = $result2->fetch_assoc();
 	
    $firstname_selected = $row2['firstname'];
    $lastname_selected = $row2['lastname'];

 	echo "<option id=\"selected-season\" value=\"$playerID_selected\" selected=\"selected\"> $firstname_selected $lastname_selected is selected</option>";
 	
 } else {
 	echo "<option id=\"selected-season\" value=\"\" disabled=\"disabled\" selected=\"selected\">Select a player</option>";
 }

 ?> 
         
<?php

$sql = "SELECT DISTINCT user_key, firstname, lastname FROM users WHERE type = 'Player' OR type = 'Team' ORDER BY firstname ASC";

$result = $db->query ($sql);

while ($row = $result->fetch_assoc()) {

	 $playerID = $row['user_key'];
    $firstname = $row['firstname'];
    $lastname = $row['lastname'];
    
    $teamname = $row['teamname'];
    echo "<option value=\"$playerID\">$firstname $lastname</option>";  
}
?>			
			
		</select>

		<br>

      <select name="playertype" class="form-control" id="select">

<?php

if (isset($_POST['playertype'])) {

	$playertype = $_POST['playertype'];

	echo "<option value='$playertype' selected='selected'>". ucfirst($playertype) . " is selected </option>";

} else {
	echo "<option value='' selected='selected' disabled='disabled'> Select playertype </option>";	
}


?>
      	<option value="player"> Player </option>
      	<option value="bench"> Bench Player </option>
      	<option value="training"> Training Member </option>

      </select>

		<br>

		<div class="input-group">
        <input class="form-control" id="ex1" type="number" placeholder="playernumber" name="playernumber">
       <span class="input-group-addon"></span>
      </div>

      

		<br>

		<div class="form-group">        
      <div class="col-lg-offset-4 col-md-offset-4 col-lg-4 col-md-4">
        <button type="submit" class="btn btn-lg btn-success btn-block" id="submit" name="submit" value="submit"> Add Player</button>
      </div>

	</form>

	<br>

<?php


if (isset($_POST['season']) && isset($_POST['teamname']) && isset($_POST['playerID']) && isset($_POST['playernumber']) && $_POST['playernumber'] !== '') {

  foreach ($_POST as $key => $value) {
      if ($value == '') {
        $value = ":null";
        $$key = $value;
      } else {
        	$$key = $value;
      }
    }

	$basketbalhandlerfunctions->AddPlayerToOpponentTeam($season, $teamname, $playerID, $playernumber,$playertype);

}

?>


</div>
<div class="col-lg-3 col-md-3"></div>
</div>
</div>

<script type="text/javascript">
	
$("#form select").on("change", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
        type: "POST",
        url: "./admin_addplayertoopponent.php",
        data: postData,
        success: function(data) { 
            $("#formframe").html(data); 
        },
        error: function (xhr, status, error) {
        	$("#formframe").html(xhr.responseText); 
        }
      }); 
    e.preventDefault();
});


$("#submit").on("click", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
		type: "POST",
		url: "./admin_addplayertoopponent.php",
		data: postData,
		success: function(data) {
			$("#formframe").html(data);
		},
		error: function (xhr, status, error) {
			$("#formframe").html(xhr.responseText); 
		}
  	}); 
  e.preventDefault();
});

</script>

</body>
</html>